Skip to content

fix: use regex exact match in get_current_app to prevent package substring misidentification#415

Open
nankingjing wants to merge 1 commit into
zai-org:mainfrom
nankingjing:fix/get-current-app-substr
Open

fix: use regex exact match in get_current_app to prevent package substring misidentification#415
nankingjing wants to merge 1 commit into
zai-org:mainfrom
nankingjing:fix/get-current-app-substr

Conversation

@nankingjing

Copy link
Copy Markdown

Problem

phone_agent/adb/device.py get_current_app() uses substring matching (if package in line) to identify the running app from dumpsys window output. This causes misidentification when one package name is a substring of another.

Concrete example

The APP_PACKAGES dict contains:

  • "GoogleDrive": "com.google.android.apps.docs" (Google Drive)
  • "Google Docs": "com.google.android.apps.docs.editors.docs" (Google Docs)
  • "GoogleSlides": "com.google.android.apps.docs.editors.slides" (Google Slides)

When Google Docs is running, dumpsys window outputs:

mCurrentFocus=Window{abc123 u0 com.google.android.apps.docs.editors.docs/...}

The substring check "com.google.android.apps.docs" in line evaluates to True, so it incorrectly returns "Google Drive" (which appears first in dict insertion order) instead of "Google Docs". The same misidentification occurs with Google Slides.

Fix

Replace substring matching with regex-based exact package extraction followed by exact dict lookup. The regex r"\s(\w+)\s+([\w.]+)/" extracts the package portion from the dumpsys window format (Window{... u0 com.example.app/...}).

A substring-matching fallback is kept for unusual dumpsys formats where the regex may not match.

Verification

  • python -m py_compile passes on the changed file
  • Regex tested against sample dumpsys window lines — correctly extracts com.google.android.apps.docs.editors.docs for Docs vs com.google.android.apps.docs for Drive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant